package org.uni_hi.lsep.gui.test.sim;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.uni_hi.lsep.gui.util.VerifiedTextField;
import org.uni_hi.lsep.util.PortAddress;
import org.uni_hi.lsep.util.PortAddressSyntaxException;

public class PortInputVerifier extends InputVerifier {

	@Override
	public boolean verify(JComponent jComp) {
		JTextField tf=(JTextField) jComp;
		String port=tf.getText();
		if(port.equals(""))return true;
		PortAddress adr=null;
		try {
			adr=PortAddress.parse(port,jComp.getName());
			((VerifiedTextField) tf.getParent()).setSelectedItem(adr);
		} catch (PortAddressSyntaxException e) {
			JOptionPane.showMessageDialog(jComp.getRootPane(), e.getMessage(), "Warnung", JOptionPane.WARNING_MESSAGE);
			tf.selectAll();
		}
		return adr!=null;
	}
}
